---
root: "/v5.1"
layout: "default"
keywords: ""
title: "swap"
description: "Swift documentation for 'swap'"
---

<div class="intro-declaration"><code class="language-swift">func swap(_:_:)(_ a: inout T, _ b: inout T)</code></div><div class="discussion comment"><p>Exchanges the values of the two arguments.
</p>
<p>The two arguments must not alias each other. To swap two elements of a
mutable collection, use the <code>swapAt(_:_:)</code> method of that collection
instead of this function.</p>
</div>